import logging

import config


class RegLoginApi:

    def __init__(self, session):
        self.session = session

    # 获取图片验证码接口
    def get_img_verify_code(self, r):
        url = config.BASE_HOST + "/common/public/verifycode1/{}".format(r)
        resp = self.session.get(url=url)
        return resp

    # 获取短信验证码接口
    def get_phone_verify_code(self, phone, imgVerifyCode, type="reg"):
        url = config.BASE_HOST + "/member/public/sendSms"
        info = {"phone": phone, "imgVerifyCode": imgVerifyCode, "type": type}
        resp = self.session.post(url=url, data=info)
        logging.info(f'获取短信验证码{resp.json()}')
        return resp

    # 注册接口
    def user_register(self, phone, password, verifycode, phone_code, dy_server, invite_phone=None):
        url = config.BASE_HOST + "/member/public/reg"
        info = {"phone": phone, "password": password, "verifycode": verifycode,
                "phone_code": phone_code, "dy_server": dy_server, "invite_phone": invite_phone}
        resp = self.session.post(url=url, data=info)
        logging.info(f'注册{resp.json()}')
        return resp

    # 登录接口
    def user_login(self, keywords, password):
        url = config.BASE_HOST + "/member/public/login"
        info = {"keywords": keywords, "password": password}
        resp = self.session.post(url=url, data=info)
        logging.info(f'登录{resp.json()}')
        return resp
